package org.example.controller;


import lombok.Data;
import org.example.entity.Img;
import org.example.entity.Strategy;
import org.example.mapper.ImgMapper;
import org.example.mapper.StrategyMapper;
import org.example.service.IImgService;
import org.example.service.IStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author example.demo
 * @since 2023-08-06
 */
@RestController
@RequestMapping("/strategy")
public class StrategyController {
    @Autowired
    IStrategyService iStrategyService;
    @Autowired
    IImgService iImgService;
    @Autowired
    StrategyMapper strategyMapper;
    @Autowired
    ImgMapper imgMapper;


    @GetMapping("/strategy")
    List<Strategy> strategy() {
        List<Strategy> list1=new ArrayList<>();
        List<Strategy> list=iStrategyService.list();
        for(int i=0;i<list.size();i++){
            list1.add(i,list.get(i));
            if(i==3){
                break;
            }
        }
        return list1;
    }
    @GetMapping("/strategy2")
    List<Strategy> strategy2() {
        return iStrategyService.list();
    }

    @RequestMapping("submit")
    boolean submit(@RequestParam("userid") String userid, @RequestParam("title") String title, @RequestParam("place") String place,
                   @RequestParam("content") String content, @RequestParam(value = "urlList") List<String> urlList) {
        int visitor = 0;
        LocalDate time = LocalDate.now();
        boolean b1 = strategyMapper.adds(userid, title, place, content, time, visitor,urlList.get(0));
        int sId = iStrategyService.lambdaQuery().eq(Strategy::getUserid, userid).eq(Strategy::getTitle, title).eq(Strategy::getPlace, place).eq(
                Strategy::getContent, content).list().get(0).getId();
        Img img = new Img();
        System.out.println(urlList);
        for (int i = 0; i < urlList.size(); i++) {
            img.setSid(sId);
            img.setPicture(urlList.get(i));
            System.out.println(img);
//            b1=b1&iImgService.save(img);
            b1=b1&imgMapper.addi(sId,urlList.get(i));
        }
        return b1;
    }
    @GetMapping("/showhot")
    List<Strategy> showhot(){
        List<Strategy> list=iStrategyService.list();
        for(int i=0;i<list.size()-1;i++){
            for (int j=i+1;j<list.size();j++){
                int max=list.get(i).getVisitor();
                if(list.get(j).getVisitor()>max){
                    max=list.get(j).getVisitor();
                    list.get(j).setVisitor(list.get(i).getVisitor());
                    list.get(i).setVisitor(max);
                }
            }
        }
        System.out.println("00000000000000000000000000000000000000000000000000");
        System.out.println(list);
        return list;
    }



}
